package com.itbac.springsecurityoauth2demo.config;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.Map;

/**
 * JWT 内容增强 ,需要什么，就传个Map进去
 * @author: BacHe
 * @email: 1218585258@qq.com
 * @Date: 2021/3/27 18:01
 */
public class JwtTokenEnhancer implements TokenEnhancer{

    private Map<String, Object> map;

    public JwtTokenEnhancer(Map<String, Object> map) {
        this.map = map;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public JwtTokenEnhancer setMap(Map<String, Object> map) {
        this.map = map;
        return this;
    }

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken,
                                     OAuth2Authentication oAuth2Authentication) {
        //把 map 传进去
        ((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(this.map);
        return oAuth2AccessToken;
    }
}
